package caidan.data;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import jinyilw.common.Alignment;
import jinyilw.swing.FontTools;
import jinyilw.common.Operator;
import jinyilw.swing.SwingTools;
import jinyilw.swing.rlaf.utils.RBorderFactory;
import jinyilw.swing.visuals.layout.OneRowLayout;

public class RadioPanel extends JPanel
{
	private static final long serialVersionUID = 3197204325468491092L;

	private JRadioButton[] buttons;

	public RadioPanel(String description, String[] contents,
			Operator<Short> operator)
	{
		super(new OneRowLayout(Alignment.LEFT));
		setBorder(RBorderFactory.getDefaultBorder());
		Font font = FontTools.getSystemFont(12);
		JLabel dLabel = new JLabel(description);
		dLabel.setFont(font);
		add(dLabel);
		buttons = new JRadioButton[contents.length];
		ButtonGroup buttongroup = new ButtonGroup();
		for (short i = 0; i < buttons.length; i++)
		{
			buttons[i] = new JRadioButton(contents[i]);
			buttons[i].setFont(font);
			add(buttons[i]);
			buttongroup.add(buttons[i]);
			short ii = i;
			buttons[i].addActionListener(new ActionListener()
			{
				short j = ii;
				@Override
				public void actionPerformed(ActionEvent e)
				{
					if (buttons[j].isSelected())
						operator.operate(j);
				}
			});
			buttons[i].setFocusable(false);
		}
	}

	public void setData(short d)
	{
		if (d >= 0)
			buttons[d].setSelected(true);
		else
			SwingTools.selectNoneRadioButtons(buttons);
	}
}
